package com.redwood.report.adapter;

import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.util.Log;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.redwood.report.R;
import com.redwood.report.entity.BaoBiaoBeen;
import com.redwood.report.entity.BarChartBeen;
import com.redwood.report.entity.BarDataBeen;
import com.redwood.report.entity.PieChartBeen;
import com.redwood.report.entity.bak.WxJiluInfo;
import com.redwood.report.util.Utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 报表item
 *
 * Created by Supeng on 2018/8/23.
 */
//Caption been
public class ReportItemAdapter extends BaseMultiItemQuickAdapter<MultiItemEntity,BaseViewHolder> {
    public static final int TYPE_LEVEL_1 = 0;
    public static final int TYPE_LEVEL_2 = 1;
    public static final int TYPE_LEVEL_3 = 2;
    public ReportItemAdapter(List data) {
        super(data);
        addItemType(TYPE_LEVEL_1, R.layout.layout_report1_item);
        addItemType(TYPE_LEVEL_2, R.layout.layout_report2_item);
        addItemType(TYPE_LEVEL_3, R.layout.layout_report3_item);
    }
    @Override
    protected void convert(final BaseViewHolder holder, MultiItemEntity itemTemp) {
        switch (holder.getItemViewType()) {
            case TYPE_LEVEL_1:
                BaoBiaoBeen baoBiaoBeen= (BaoBiaoBeen) itemTemp;
                int xvhao=(holder.getAdapterPosition())-1;
                if(baoBiaoBeen.getIsTitle()){
                    holder.setText(R.id.tv_xvhao,"序号");
                    holder.setText(R.id.tv_startTimer,"开始");
                    holder.setText(R.id.tv_endTimer,"结束");
                    holder.setText(R.id.tv_longTimer,"时长");
                    holder.setText(R.id.tv_zhuangtai,"状态");
                    holder.setText(R.id.tv_yuanying,"原因");
                    holder.setText(R.id.tv_caozuoren,"操作人");
                }else{
                    holder.setText(R.id.tv_xvhao,xvhao+"");
                    holder.setText(R.id.tv_startTimer,baoBiaoBeen.getStartTimer()+"");
                    holder.setText(R.id.tv_endTimer,baoBiaoBeen.getEndTimer()+"");
                    holder.setText(R.id.tv_longTimer,baoBiaoBeen.getLongTimer()+"");
                    holder.setText(R.id.tv_zhuangtai,baoBiaoBeen.getZhuangtai()+"");
                    holder.setText(R.id.tv_yuanying,baoBiaoBeen.getYuanying()+"");
                    holder.setText(R.id.tv_caozuoren,baoBiaoBeen.getCaozuoren()+"");
                }
                if(xvhao%2!=0){
                    holder.setBackgroundColor(R.id.ll_item_background,mContext.getResources().getColor(R.color.blueColorAppKey3));
                }else{
                    holder.setBackgroundColor(R.id.ll_item_background,mContext.getResources().getColor(R.color.blueColorAppKey));
                }
               break;
            case TYPE_LEVEL_2:
                PieChartBeen pieChartBeen= (PieChartBeen) itemTemp;
                PieChart chart_jitai=holder.getView(R.id.chart_jitai);
                initChartOne(chart_jitai);
                setTSData(chart_jitai,pieChartBeen.getZaixian(),pieChartBeen.getLixian(),pieChartBeen.getJingdao(),pieChartBeen.getDaiji());
                break;
            case TYPE_LEVEL_3:
                BarChartBeen barChartBeen= (BarChartBeen) itemTemp;
                BarChart chart_barchart_jitai=holder.getView(R.id.chart_barchart_jitai);
                initChartSet(chart_barchart_jitai);
                initChart(barChartBeen.getBarDataBeenList(),chart_barchart_jitai);
                break;
        }

    }

    private void initChart(List<BarDataBeen> barDataBeenList, BarChart chart_barchart_jitai){
        if(barDataBeenList.size()==0){
            return;
        }
        float[] valueArr = new float[barDataBeenList.size()];
        int[] colorArr = new int[barDataBeenList.size()];
        for(int i=0;i<barDataBeenList.size();i++){
            valueArr[i]=barDataBeenList.get(i).getTimer();
            colorArr[i]=barDataBeenList.get(i).getColor();
        }
        List<BarEntry> yvalue=new ArrayList<>();
        //敲黑板啦！！这里才是重点部分，可以添加一个float数组，让它变成StackBar
        yvalue.add(new BarEntry(valueArr,0));
        //   yvalue.add(new BarEntry(new float[]{4f,5f},0));
        // yvalue.add(new BarEntry(new float[]{12,95},2));
        // yvalue.add(new BarEntry(new float[]{122,195},3));
        BarDataSet set=new BarDataSet(yvalue,"");
        set.setColors(colorArr);//set会循环这里的颜色进行添加
        List<String> xvalue=new ArrayList<>();
        xvalue.add("M001");
        //   xvalue.add("M002");
        //  xvalue.add("第三季度");
        //  xvalue.add("第四季度");
        set.setDrawValues(false);
        BarData data=new BarData(xvalue,set);
        chart_barchart_jitai.setData(data);

    }
    private void initChartSet( BarChart chart_barchart_jitai) {

        YAxis yAxis = chart_barchart_jitai.getAxisLeft();
        yAxis.setAxisMaxValue(24);
        yAxis.setLabelCount(9,false);
        yAxis.setAxisMinValue(0);
        yAxis.setTextColor(mContext.getResources().getColor(R.color.bwhite));
        YAxis yAxist = chart_barchart_jitai.getAxisRight();
        yAxist.setAxisMaxValue(24);
        yAxist.setLabelCount(24,false);
        yAxist.setAxisMinValue(0);
        yAxist.setDrawLabels(false);
        chart_barchart_jitai.getLegend().setEnabled(false);
        chart_barchart_jitai.animateY(1500);
        chart_barchart_jitai.setDescription("");
        chart_barchart_jitai.getXAxis().setDrawLabels(false);

        //.setPosition(XAxis.XAxisPosition.BOTTOM);
        //.setDrawLabels(false);
    }

    private void setTSData(PieChart chart_jitai,float yunxing,float lixian,float jinggao,float daiji) {
        int allCount=(int)(yunxing+lixian+jinggao+daiji);
        chart_jitai.setCenterText(generateCenterText());
        //设置标题
        ArrayList<String> titles = new ArrayList<>();
        ArrayList<Entry> entrys = new ArrayList<>();
        int index=0;
        List<Integer> colors =new ArrayList<>();
        if(yunxing!=0){
            titles.add(mContext.getResources().getString(R.string.yunxing)+(int)((yunxing/allCount)*1000)/10f+"%");
            if(yunxing<0.01){
                yunxing=0.01f;
            }
            entrys.add(new Entry(yunxing, index));
            colors.add(mContext.getResources().getColor(R.color.main_green));
            index++;
        }
        if(lixian!=0){
            titles.add(mContext.getResources().getString(R.string.lixian)+(int)((lixian/allCount)*1000)/10f+"%");
            if(lixian<0.01){
                lixian=0.01f;
            }
            entrys.add(new Entry(lixian, index));
            colors.add(mContext.getResources().getColor(R.color.main_gray));
            index++;
        }
        if(jinggao!=0){
            titles.add(mContext.getResources().getString(R.string.jinggao)+(int)((jinggao/allCount)*1000)/10f+"%");
            if(jinggao<0.01){
                jinggao=0.01f;
            }
            entrys.add(new Entry(jinggao, index));
            colors.add(mContext.getResources().getColor(R.color.main_red));
            index++;
        }
        if(daiji!=0){
            titles.add(mContext.getResources().getString(R.string.daiji)+(int)((daiji/allCount)*1000)/10f+"%");
            if(daiji<0.01){
                daiji=0.01f;
            }
            entrys.add(new Entry(daiji, index));
            colors.add(mContext.getResources().getColor(R.color.main_yellow));
            index++;
        }

        //饼图数据集
        PieDataSet dataset = new PieDataSet(entrys, "");
        //设置饼状图Item之间的间隙
        dataset.setSliceSpace(5f);
        dataset.setDrawValues(false);
        //饼图Item被选中时变化的距离
        dataset.setSelectionShift(5f);
//        dataset.setValueFormatter(new ValueFormatter() {//圆环内文字设置
//            @Override
//            public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
//                int n = (int) value;
//
//                String str = n + "台";
//                if (n == 0) {
//                    str = "";
//                }
//                return str;
//            }
//        });
        //颜色填充
        dataset.setColors(colors);
        //数据填充
        PieData piedata = new PieData(titles,dataset);
        //设置饼图上显示数据的字体大小
        piedata.setValueTextSize(12);
        piedata.setValueTextColor(mContext.getResources().getColor(R.color.bwhite));
        chart_jitai.setData(piedata);
       // chart_jitai.invalidate();
    }
    /**
     * 中间文字绘制
     *
     * @param
     * @return
     */
    private SpannableString generateCenterText() {
       // String total = Integer.toString(sum);
        SpannableString s = new SpannableString("设备运行情况");
      //  s.setSpan(new RelativeSizeSpan(3f), 0, total.length(), 0);
      //  s.setSpan(new ForegroundColorSpan(Color.rgb(88, 146, 240)), 0, total.length(), 0);
        s.setSpan(new ForegroundColorSpan(Color.rgb(153, 153, 153)), 0, s.length(), 0);
        return s;
    }
    private void initChartOne(PieChart chart) {
        //设置是否可转动
        chart.setRotationEnabled(false);
        //设置X轴的动画
        chart.animateX(1000);
        //使用百分比
        chart.setUsePercentValues(true);
        //设置图列可见
        chart.getLegend().setEnabled(false);
        chart.setDrawHoleEnabled(true);
        chart.setHoleColor(mContext.getResources().getColor(R.color.transt));
        chart.setDrawSliceText(true);
        chart.setDescription("");
        chart.setBackgroundColor(mContext.getResources().getColor(R.color.blueColorAppKey));
        chart.setCenterTextSize(12f);//设置中间文字中大小

    }
    public void setText(TextView tvCount,String context,String img) {
        String counStr="";
        if(!TextUtils.isEmpty(img)){
            String[] arrimgs=img.split("\\^");
            counStr=context + "照片("+arrimgs.length+")";
            SpannableStringBuilder style = new SpannableStringBuilder(counStr);
            style.setSpan(new ForegroundColorSpan(mContext.getResources().getColor(R.color.blueColorAppKey)), context.length(), counStr.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE); //颜色
            tvCount.setText(style);
        }else{
            tvCount.setText(context);
        }

    }
}
